/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dnbW.uiW.dbW;

import dnbW.dbW.DatabaseInfo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;


/**
 *
 * @author zhouyong
 */
public class AddDatabaseTask {

    private DatabaseInfo databaseInfo;
    private List<String> dataSourceList;

//    public AddDatabaseTask(
//            DNB application,
//            DatabaseInfo db) {
//        //super(application);
//        this.databaseInfo = db;
//    }

    public AddDatabaseTask(DatabaseInfo databaseInfo, List<String> dataSourceList) {
        //super(DNB.getApplication());
        this.databaseInfo = databaseInfo;
        this.dataSourceList = dataSourceList;
    }

//    @Override
//    protected void failed(Throwable arg0) {
//        super.failed(arg0);
//        setMessage("Database Creation Failed: " + databaseInfo.getDbName());
//    }
//
//    @Override
//    protected void succeeded(Void arg0) {
//        setMessage("Database: " + databaseInfo.getDbName() + "  created successfully...");
//    }
//
//    @Override
//    protected Void doInBackground() throws Exception {
//
//        setMessage("Adding Database: " + databaseInfo.getDbName());
//        try {
//            DNB.getDatabaseController().addDatabase(databaseInfo);
//        } catch (Exception ex) {
//            setMessage("Database Creation Failed: " + databaseInfo.getDbName());
//            try {
//                String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=master";
//                Connection connection = DriverManager.getConnection(
//                        url,
//                        databaseInfo.getUserName(),
//                        databaseInfo.getPassword());
//                Statement statement = connection.createStatement();
//                String sql =
//                        "select name\n" +
//                        "from sys.databases\n" +
//                        "where name = '" + databaseInfo.getDbName() + "'";
//                ResultSet result = statement.executeQuery(sql);
//
//                while (result.next()) {
//                    statement.execute("drop database '" + result.getString(1) + "'");
//                }
//            } catch (Exception e) {
//                ex.printStackTrace();
//            }
//            throw ex;
//        }
//
//        for(String dataSource : dataSourceList){
//            databaseInfo.addDataSource(dataSource);
//        }
//        
//        return null;
//    }

    public DatabaseInfo getDatabaseInfo() {
        return databaseInfo;
    }

    public void setDatabaseInfo(DatabaseInfo databaseInfo) {
        this.databaseInfo = databaseInfo;
    }
}
